package com.runda.system.controller;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.bbjob.dto.Result;
import com.bbjob.model.User;
import com.runda.entity.JwtUser;
import com.runda.sec.SecContextUtil;
import com.runda.system.service.UserService;
import com.runda.util.DataEntity;


@RestController
@RequestMapping("sys/user")
public class UserController {
	
	@Resource
	private UserService userservice;
	
	@RequestMapping("/aaa")
	@ResponseBody
	public String home() {
		StringBuffer sb= new StringBuffer();
		sb.append("<form id='form1' action='http://127.0.0.1:8888/upload' method='post' enctype='multipart/form-data'>");   
		sb.append("文件名<input id='name' name='name' type='text' value='root.pdf'/></br>");   
		//sb.append("总大小<input id='size' name='size' type='text'value='10000'/></br>");   
		sb.append("总片数<input id='total' name='total' type='text'value='1'/></br>");   
		sb.append("当前片数<input id='index' name='index' type='text'value='1'/></br>");
		sb.append("资源等级<input id='index' name='resLevel' type='text'value='1'/></br>");   
		sb.append("总MD5<input id='fileMd5' name='fileMd5' type='text'value='ffec78f47b76096e64c381412ae82d1'/></br>");   
		sb.append("分片MD5<input id='md5' name='md5' type='text'value='ffec78f47b76096e64c381412ae82d1'/></br>");   
		sb.append("上传的文件<input id='file' name='file' type='file' /></br>");   
		sb.append("<input type='submit' value='提交' />");   
		return sb.toString();
	}
	
	
	@RequestMapping(value = "saveUser", method = RequestMethod.POST)
	@ResponseBody
	public Result saveUser(@RequestBody User user) {
		int ok = userservice.saveUser(user);
		if(ok > 0) {
			return new Result(200, "保存成功");
		}else if(ok == -1) {
			return new Result(8106,"用户名已存在");
		}else {
			return new Result(8106,"保存失败");
		}
	}
	
	@RequestMapping(value = "editUser", method = RequestMethod.POST)
	@ResponseBody
	public Result editUser(@RequestBody User user) {
		int ok = userservice.editUser(user);
		if(ok > 0) {
			return new Result(200, "修改成功");
		}else {
			return new Result(8106,"修改失败");
		}
	}
	
	@RequestMapping(value = "deleteUser", method = RequestMethod.GET)
	@ResponseBody
	public Result deleteUser(Integer id) {
		int ok = userservice.deleteUser(id);
		if(ok > 0) {
			return new Result(200, "删除成功");
		}else {
			return new Result(8106,"删除失败");
		}
	}
	
	@RequestMapping(value = "getList", method = RequestMethod.GET)
	@ResponseBody
	public DataEntity getList(User user, @RequestParam(required = false, defaultValue = "1") int page,
			@RequestParam(required = false, defaultValue = "10") int rows) {
		return DataEntity.getInstance(userservice.getlist(user, page, rows));
	}
	
	/**
	 * 给用户赋予角色权限
	 * @param userid
	 * @param roleid
	 * @return
	 */
	@RequestMapping(value = "authUser", method = RequestMethod.GET)
	@ResponseBody
	public Result authUser(@RequestParam(required = true) Integer userid,@RequestParam(required = true) String roleid) {
		int ok = userservice.setUserToRole(userid, roleid);
		if(ok > 0) {
			return new Result(200, "操作成功");
		}else {
			return new Result(8106,"操作失败");
		}
	}
	
	/**
	 * 用户获取角色
	 * @param userid
	 * @return
	 */
	@RequestMapping(value = "getauth", method = RequestMethod.GET)
	@ResponseBody
	public DataEntity getauth(@RequestParam(required = true) Integer userid) {
		return DataEntity.getInstance(userservice.getauth(userid));
	}
	
	/**
	 * 重置密码
	 * @param userid
	 * @return
	 */
	@RequestMapping(value = "resetPassword", method = RequestMethod.GET)
	@ResponseBody
	public Result resetPassword(@RequestParam(required = true) Integer userid) {
		int ok = userservice.resetPassword(userid);
		if(ok > 0) {
			return new Result(200, "操作成功");
		}else {
			return new Result(8106,"操作失败");
		}
	}
	
	/**
	 * 用户修改密码
	 * @param userid
	 * @return
	 */
	@RequestMapping(value = "changePassword", method = RequestMethod.GET)
	@ResponseBody
	public Result changePassword(@RequestParam(required = true) Integer userid,@RequestParam(required = true) String oldpassword,@RequestParam(required = true) String newpassword) {
		int ok = userservice.changePassword(userid, oldpassword, newpassword);
		if(ok > 0) {
			return new Result(200, "操作成功");
		}else if(ok ==-1){
			return new Result(8106,"无法找到用户");
		}else if(ok == -2) {
			return new Result(8106,"原密码输入错误");
		}else {
			return new Result(8106,"操作失败");
		}
	}
	
	/**
	 * 教务主任创建本学校教师
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "saveTeacher", method = RequestMethod.POST)
	@ResponseBody
	public Result saveTeacher(@RequestBody User user) {
		JwtUser jwtuser = SecContextUtil.getCurrentUser();
		int ok = userservice.createUserByDirector(user, jwtuser.getId());
		if(ok > 0) {
			return new Result(200, "保存成功");
		}else if(ok == -1) {
			return new Result(8106,"用户名已存在");
		}else {
			return new Result(8106,"保存失败");
		}
	}
	
	/**
	 * 教务主任修改教师信息
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "editTeacher", method = RequestMethod.POST)
	@ResponseBody
	public Result editTeacher(@RequestBody User user) {
		SecContextUtil.getCurrentUser();
		int ok = userservice.editTeacher(user);
		if(ok > 0) {
			return new Result(200, "保存成功");
		}else if(ok == -1) {
			return new Result(8106,"用户名已存在");
		}else {
			return new Result(8106,"保存失败");
		}
	}
	
	/**
	 * 教务主任获取本学校教师列表
	 * @param user
	 * @param page
	 * @param rows
	 * @return
	 */
	@RequestMapping(value = "getTeacherList", method = RequestMethod.GET)
	@ResponseBody
	public DataEntity getTeacherList(User user, @RequestParam(required = false, defaultValue = "1") int page,
			@RequestParam(required = false, defaultValue = "10") int rows) {
		JwtUser jwtuser = SecContextUtil.getCurrentUser();
		return DataEntity.getInstance(userservice.getTeacherList(user, page, rows,jwtuser.getId()));
	}
	
	/**
	 * 教导主任删除本学校教师账号
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "deleteTeacher", method = RequestMethod.GET)
	@ResponseBody
	public Result deleteTeacher(Integer id) {
		JwtUser jwtuser = SecContextUtil.getCurrentUser();
		int ok = userservice.deleteTeacher(id, jwtuser.getId());
		switch (ok) {
		case 0:
			return new Result(8106,"找不到用户");
		case 1:
			return new Result(200,"删除成功");
		case -1:
			return new Result(8106,"不能删除当前用户");
		case -2:
			return new Result(8106,"不能删除主任账户");
		case -3:
			return new Result(8106,"非法数据");
		default:
			return new Result(8106,"未知错误");
		}
	}
}
